import 'package:rjpb/utils/http_util.dart';
import 'package:rjpb/config/config.dart';

class InspectionService {
  static Future<Map<String, dynamic>> fetchInspectionData({
    required int pageNum,
    required int pageSize,
  }) async {
    final url = '$baseUrl/wgq/scheme/list';
    try {
      final response = await HttpUtil().get(
        url,
        data: {
          "pageNum": pageNum,
          "pageSize": pageSize,
        },
      );
      print("获取巡检数据hahaha: $response");

      return response as Map<String, dynamic>;
    } catch (e) {
      print("请求失败: $e");
      return {
        "code": 500,
        "msg": "请求失败",
        "rows": [],
      };
    }
  }

  /// 获取菜单数据
  static Future<List<Map<String, dynamic>>> fetchMenuData() async {
    const url = '$baseUrl/wgq/location/cascadeData';
    try {
      // final response = await HttpUtil().get(url);
      final response = await HttpUtil().get(
        url,
        data: {
          "rank": 4,
        },
      );
      print("获取菜单数据: $response");
      if (response['code'] == 200 && response['data'] is List) {
        // 将返回的 data 转换为 List<Map<String, dynamic>>
        print("菜单数据: ${response['data']}");
        return List<Map<String, dynamic>>.from(response['data']);
      } else {
        print("菜单数据格式错误: ${response['msg']}");
        return [];
      }
    } catch (e) {
      print("请求失败: $e");
      return [];
    }
  }

  //根据选中菜单的Id请求对应的svg图
  static Future<String?> fetchSvgData(String menuId) async {
    final url = '$baseUrl/wgq/location/$menuId';
    print("请求SVG图的URL: $url");
    try {
      final response = await HttpUtil().get(url);
      if (response['code'] == 200 && response['data'] != null) {
        print("SVG数据: ${response['data']}");
        return response['data']['spare1'] as String?;
      } else {
        print("获取SVG数据失败: ${response['msg']}");
        return null;
      }
    } catch (e) {
      print("请求失败: $e");
      return null;
    }
  }

  //获取选中菜单对应的svg图上的点位
  static Future<List<Map<String, dynamic>>> fetchPointData(
      String locationId) async {
    print("重要: $locationId");
    final url = '$baseUrl/wgq/management/findPatrolItemByLocId';
    try {
      final response = await HttpUtil().get(
        url,
        data: {
          "locationIds": locationId,
        },
      );
      print("获取点位数据: $response");
      if (response['code'] == 200 && response['data'] is List) {
        print("点位数据: ${response['data']}");
        return List<Map<String, dynamic>>.from(response['data']);
      } else {
        print("获取点位数据失败: ${response['msg']}");
        return [];
      }
    } catch (e) {
      print("请求失败: $e");
      return [];
    }
  }

  /// 1  2
  // static Future<String?> findInspectedPatrolItemByLocId(
  //     String patrolItemId) async {
  //   final url = '$baseUrl/wgq/management/findInspectedPatrolItemByLocId';
  //   try {
  //     final response = await HttpUtil().get(
  //       url,
  //       data: {
  //         "patrolItemId": patrolItemId,
  //       },
  //     );
  //     print("获取巡检项数据: $response");
  //     if (response['code'] == 200 && response['data'] != null) {
  //       print("获取巡检项数据成功: ${response['data']}");
  //       return response['data'];
  //     } else {
  //       print("获取巡检项数据失败: ${response['msg']}");
  //       return null;
  //     }
  //   } catch (e) {
  //     print("请求失败: $e");
  //     return null;
  //   }
  // }

  static Future<Map<String, dynamic>?> findInspectedPatrolItemByLocId(
      String patrolItemId) async {
    final url = '$baseUrl/wgq/management/findInspectedPatrolItemByLocId';
    try {
      final response = await HttpUtil().get(
        url,
        data: {
          "patrolItemId": patrolItemId,
        },
      );
      print("获取巡检项数据: $response");
      if (response['code'] == 200 && response['data'] != null) {
        print("获取巡检项数据成功: ${response['data']}");
        return response['data'] as Map<String, dynamic>; // 返回 Map 类型
      } else {
        print("获取巡检项数据失败: ${response['msg']}");
        return null;
      }
    } catch (e) {
      print("请求失败: $e");
      return null;
    }
  }

  /// 3 4
  // static Future<String?> findFaultPatrolItemByLocId(String patrolItemId) async {
  //   final url = '$baseUrl/wgq/management/findFaultPatrolItemByLocId';
  //   try {
  //     final response = await HttpUtil().get(
  //       url,
  //       data: {
  //         "patrolItemId": patrolItemId,
  //       },
  //     );
  //     if (response['code'] == 200 && response['data'] != null) {
  //       print("获取故障巡检项数据成功: ${response['data']}");
  //       return response['data'] as String; // 返回 data 字段内容
  //     } else {
  //       print("获取故障巡检项数据失败: ${response['msg']}");
  //       return null;
  //     }
  //   } catch (e) {
  //     print("请求失败: $e");
  //     return null;
  //   }
  // }

  static Future<Map<String, dynamic>?> findFaultPatrolItemByLocId(
      String patrolItemId) async {
    final url = '$baseUrl/wgq/management/findFaultPatrolItemByLocId';
    try {
      final response = await HttpUtil().get(
        url,
        data: {
          "patrolItemId": patrolItemId,
        },
      );
      print("获取故障巡检项数据: $response");
      if (response['code'] == 200 && response['data'] != null) {
        print("获取故障巡检项数据成功: ${response['data']}");
        return response['data'] as Map<String, dynamic>; // 返回 Map 类型
      } else {
        print("获取故障巡检项数据失败: ${response['msg']}");
        return null;
      }
    } catch (e) {
      print("请求失败: $e");
      return null;
    }
  }

  //统计园区的点位情况
  static Future<Map<String, dynamic>> fetchHomePageStatistics() async {
    const url = '$baseUrl/wgq/inspection/homePageInformationStatistics2';
    try {
      final response = await HttpUtil().get(url);
      if (response['code'] == 200 && response['data'] != null) {
        print("获取统计数据成功: ${response['data']}");
        return response['data'] as Map<String, dynamic>;
      } else {
        print("获取统计数据失败: ${response['msg']}");
        return {
          "normal": 0,
          "repair": 0,
          "unexamined": 0,
        };
      }
    } catch (e) {
      print("请求统计数据接口失败: $e");
      return {
        "normal": 0,
        "repair": 0,
        "unfinished": 0,
      };
    }
  }
}
